﻿using System;
using System.Windows;
using System.Windows.Input;
using Awesomium.Core;
using Peter.Logic;

namespace Peter.View
{
   /// <summary>
   /// Interaction logic for ViewWeb.xaml
   /// </summary>
   public partial class ViewWeb
   {
      private ModelWeb m_ViewModel;

      /// <summary>
      /// Initializes a new web view.
      /// </summary>
      public ViewWeb ()
      {
         this.DataContextChanged += this.OnDataContextChanged;
         InitializeComponent ();
         this.WebControl.TitleChanged += this.OnTitleChanged;
         this.WebControl.DocumentReady += this.OnDocumentReady;
         this.WebControl.ShowCreatedWebView += this.OnNewView;
      }

      private void OnNewView (object sender, ShowCreatedWebViewEventArgs e)
      {
         if (this.m_ViewModel == null) return;
         this.m_ViewModel.NewWebView (e.TargetURL);
      }

      /// <summary>
      /// Occurs when the data context changes.
      /// </summary>
      /// <param name="sender">This</param>
      /// <param name="e">DependencyPropertyChangedEventArgs</param>
      private void OnDataContextChanged (object sender, DependencyPropertyChangedEventArgs e)
      {
         this.DataContextChanged -= this.OnDataContextChanged;
         this.m_ViewModel = this.DataContext as ModelWeb;
         if (this.m_ViewModel != null)
         {
            if (this.m_ViewModel.Url != null) 
               this.WebControl.Source = this.m_ViewModel.Url;
         }
      }

      /// <summary>
      /// Occurs when the web control's document has been parsed.
      /// </summary>
      /// <param name="sender">Web Control</param>
      /// <param name="e">DocumentReadyEventArgs</param>
      private void OnDocumentReady (object sender, DocumentReadyEventArgs e)
      {
         if (this.m_ViewModel == null) return;
         this.m_ViewModel.Url = this.WebControl.Source;
         //this.m_ViewModel.SetText (this.WebControl.HTML);
         this.AddressBox.Text = this.WebControl.Source.AbsoluteUri;
      }

      /// <summary>
      /// Occurs when the title of the web control has changed.
      /// </summary>
      /// <param name="sender">Web Control</param>
      /// <param name="e">TitleChangedEventArgs</param>
      private void OnTitleChanged (object sender, TitleChangedEventArgs e)
      {
         if (this.m_ViewModel == null) return;
         this.m_ViewModel.Title = this.WebControl.Title;
         this.m_ViewModel.ToolTip = this.WebControl.Source.AbsoluteUri;
      }

      /// <summary>
      /// Reloads the current web page.
      /// </summary>
      /// <param name="sender">Button</param>
      /// <param name="e">RoutedEventArgs</param>
      private void OnReload (object sender, RoutedEventArgs e)
      {
         this.WebControl.Reload (Keyboard.IsKeyDown (Key.LeftCtrl) || Keyboard.IsKeyDown (Key.RightCtrl));
      }

      /// <summary>
      /// Occurs when the user requests to navigate to a new page.
      /// </summary>
      /// <param name="sender">Button</param>
      /// <param name="e">RoutedEventArgs</param>
      private void OnNavigate (object sender, RoutedEventArgs e)
      {
         this.WebControl.Source = new Uri(this.AddressBox.Text, UriKind.RelativeOrAbsolute);
      }

      /// <summary>
      /// Navigates to the previous web page.
      /// </summary>
      /// <param name="sender">Button</param>
      /// <param name="e">RoutedEventArgs</param>
      private void OnBack (object sender, RoutedEventArgs e)
      {
         if (this.WebControl.CanGoBack())
            this.WebControl.GoBack();
      }

      /// <summary>
      /// Navigates to the next web page.
      /// </summary>
      /// <param name="sender">Button</param>
      /// <param name="e">RoutedEventArgs</param>
      private void OnForward (object sender, RoutedEventArgs e)
      {
         if (this.WebControl.CanGoForward())
            this.WebControl.GoForward();
      }

      /// <summary>
      /// Occurs when the user presses a key in the address box.
      /// </summary>
      /// <param name="sender">Text Box</param>
      /// <param name="e">KeyEventArgs</param>
      private void OnAddressKeyDown (object sender, KeyEventArgs e)
      {
         if (e.Key == Key.Enter)
            this.OnNavigate (null, null);
      }
   }
}
